package sunstar.com.xq.util

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.view.View
import android.graphics.Color.argb
import android.renderscript.Element
import android.renderscript.Element.U8_4





/**
 * Created by Sun.Star on 2017/6/15.
 */
class ScreenUtil {
    companion object{
        fun getVagueBackground(context: Activity,view:View) : Bitmap{
            return blurBitmap(screenShot(context,view),20.toFloat(),context)
        }
        fun screenShot(context: Activity,view:View) : Bitmap{
//            var view = context.window.decorView//获取顶级父view
//            //描述引用内部显示高度
//            view.layout(0,UtilBox.getStatusBarHeight(context),UtilBox.getWindowWith(context),UtilBox.getWindowHeight(context)-UtilBox.getVirtualBarHeigh(context))
            view.isDrawingCacheEnabled = true
            var bitmap = Bitmap.createBitmap(view.getDrawingCache())
            return bitmap
        }

        fun blurBitmap(bitmap: Bitmap, radius: Float, context: Context): Bitmap {
            //Let's create an empty bitmap with the same size of the bitmap we want to blur
            val outBitmap = Bitmap.createBitmap(bitmap.width, bitmap.height,
                    Bitmap.Config.ARGB_8888)

            //Instantiate a new Renderscript
            val rs = android.renderscript.RenderScript.create(context)

            //Create an Intrinsic Blur Script using the Renderscript
            val blurScript = android.renderscript.ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))

            //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
            val allIn = android.renderscript.Allocation.createFromBitmap(rs, bitmap)
            val allOut = android.renderscript.Allocation.createFromBitmap(rs, outBitmap)

            //Set the radius of the blur
            blurScript.setRadius(radius)

            //Perform the Renderscript
            blurScript.setInput(allIn)
            blurScript.forEach(allOut)

            //Copy the final bitmap created by the out Allocation to the outBitmap
            allOut.copyTo(outBitmap)

            //recycle the original bitmap
            //        bitmap.recycle();

            //After finishing everything, we destroy the Renderscript.
            //        view.setBackground(new BitmapDrawable(context.getResources(), outBitmap));
            rs.destroy()

            return outBitmap
        }
    }




}